﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataUpload
{
    public partial class FrmSummary : Form
    {
        private MainForm _container = null;

        public FrmSummary()
        {
            InitializeComponent();
        }

        public FrmSummary(MainForm container)
        {
            _container = container;
            InitializeComponent();
        }

        private void FrmSummary_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                this.Focus();
                Application.DoEvents();
            }
        }

        private void FrmSummary_Load(object sender, EventArgs e)
        {
            string uploadedFiles = "";
            string failedFiles = "";
            int rowsInserted = 0;
            int rowsFailed = 0;
            foreach (ReadFiles.ParsedFile f in _container.frmMapping.parsedFiles.Values)
            {
                if (f.Status == ReadFiles.ParsedFile.MapStatus.MappingDone)
                {
                    uploadedFiles += "\t+ " + f.FilePath + Environment.NewLine;
                    rowsInserted += (int)f.RowsInserted;
                    rowsFailed += (int)f.RowsFailed.Count;
                }
                else
                {
                    failedFiles += "\t+ " + f.FilePath;
                    failedFiles += "\t" + f.Status.ToString() + Environment.NewLine;
                }
            }

            rteSummary.SelectionColor = Color.Blue;
            rteSummary.SelectedText = "Files failed to upload:" + Environment.NewLine;
            rteSummary.SelectionColor = Color.Red;
            rteSummary.SelectedText = failedFiles;

            rteSummary.SelectionColor = Color.Blue;
            rteSummary.SelectedText = "Files uploaded:" + Environment.NewLine;
            rteSummary.AppendText(uploadedFiles);

            rteSummary.SelectionColor = Color.Blue;
            rteSummary.SelectedText = rowsInserted.ToString() + " rows inserted." + Environment.NewLine;

            rteSummary.SelectionColor = Color.Red;
            rteSummary.SelectedText = rowsFailed.ToString() + " rows failed to insert." + Environment.NewLine;

            rteSummary.AppendText("List of fail records:" + Environment.NewLine);
            foreach (ReadFiles.ParsedFile f in _container.frmMapping.parsedFiles.Values)
            {
                if (f.RowsFailed.Count > 0)
                {
                    rteSummary.SelectionColor = Color.Red;
                    rteSummary.AppendText("+ " + f.FilePath + ":" + Environment.NewLine);
                }
                for (int i = 0; i < f.RowsFailed.Count; i++)
                {
                    rteSummary.SelectionColor = Color.Blue;
                    rteSummary.SelectedText = "\t\t+ " + f.RowsFailed.GetKey(i).ToString() + Environment.NewLine;
                    rteSummary.SelectionFont = new Font(rteSummary.SelectionFont.FontFamily, rteSummary.SelectionFont.Size - 1, FontStyle.Italic);
                    rteSummary.SelectedText = "\t" + f.RowsFailed.GetByIndex(i).ToString() + Environment.NewLine + Environment.NewLine;

                    if (i % 10 == 0)
                        Application.DoEvents();
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            _container.Close();
        }
    }
}
